//
//  PushTransitionAnimation.m
//  29-TransitionAnimation
//
//  Created by 曹曹 on 16/8/23.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "PushTransitionAnimation.h"
#import "ViewController.h"
#import "SecondViewController.h"

@implementation PushTransitionAnimation

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.5f;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    ViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    SecondViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVC.view];
    toVC.toImage.hidden = YES;
    // 截图大法
    UIView *snapView = [fromVC.fromImage snapshotViewAfterScreenUpdates:NO];
    snapView.frame = fromVC.fromImage.frame;
    [containerView addSubview:snapView];
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        [containerView layoutIfNeeded];
        snapView.frame = toVC.toImage.frame;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
        toVC.toImage.hidden = NO;
        [snapView removeFromSuperview];
    }];
}
@end
